package dearchenpush.good.com.qianfanlive.modules.discover.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import dearchenpush.good.com.qianfanlive.R;
import dearchenpush.good.com.qianfanlive.modules.discover.adapter.RecyclerLikeAdapter;
import dearchenpush.good.com.qianfanlive.modules.discover.bean.RecyclerLikeBean;
import dearchenpush.good.com.qianfanlive.url.UrlConfigs;
import okhttp3.Call;

/**
 * A simple {@link Fragment} subclass.
 */
public class DiscoverLikeFragment extends Fragment {


    @BindView(R.id.recyclerView_disLikeFragment)
    RecyclerView mRecyclerViewt;
    @BindView(R.id.linearLayout_disLikeFragment_change)
    LinearLayout mChange;
    private GridLayoutManager mManager;

    private List<RecyclerLikeBean.MessageBean.AnchorsBean> mAnchors;
    private RecyclerLikeAdapter mAdapter;
    //点击按钮换一换的时候临时存放十条数据的list
    private List<RecyclerLikeBean.MessageBean.AnchorsBean> aa = new ArrayList<>();

    private boolean flag = true;

    public DiscoverLikeFragment() {

        // Required empty public constructor
    }

    public static DiscoverLikeFragment newInstance() {
        DiscoverLikeFragment fragment = new DiscoverLikeFragment();
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_discover_like, container, false);

        ButterKnife.bind(this, view);
        initData();
        setData();

        return view;
    }

    private void setData() {
        mRecyclerViewt.setLayoutManager(mManager);
        mRecyclerViewt.setAdapter(mAdapter);
        loadData();
    }

    private void initData() {
        mManager = new GridLayoutManager(getContext(), 3, LinearLayoutManager.VERTICAL, false);
        mAdapter = new RecyclerLikeAdapter(getContext());
    }

    private void loadData() {
        OkHttpUtils.get().url(UrlConfigs.DISCOVER_LIKE)
                .build().execute(new StringCallback() {


            @Override
            public void onError(Call call, Exception e, int id) {

            }

            @Override
            public void onResponse(String response, int id) {
                Gson gson = new Gson();
                RecyclerLikeBean bean = gson.fromJson(response, RecyclerLikeBean.class);
                mAnchors = bean.getMessage().getAnchors();
                for (int i = 0; i < 9; i++) {
                    aa.add(mAnchors.get(i));
                }
                mAdapter.addAll(aa);
            }
        });
    }

    @OnClick(R.id.linearLayout_disLikeFragment_change)
    public void onClick() {
        if (flag) {
            aa.clear();
            mAdapter.clearAll();
            for (int i = 9; i < 18; i++) {
                aa.add(mAnchors.get(i));
            }
            mAdapter.addAll(aa);
            flag = false;
        } else {
            aa.clear();
            mAdapter.clearAll();
            for (int i = 0; i < 9; i++) {
                aa.add(mAnchors.get(i));
            }
            mAdapter.addAll(aa);
            flag = true;
        } 
    }
}
